﻿<div class="col-md-3">
    <div class="panel panel-default" style="height:200px;">
        <h5 style="text-align:center">判断题： <input type="text" class="form-control" style="width:50px;display:inline;height:30px;" id="Judge" /> 题</h5>
        <h5 style="text-align:center">选择题： <input type="text" class="form-control" style="width:50px;display:inline;height:30px;" id="Choose" /> 题</h5>
        <h5 style="text-align:center">填空题： <input type="text" class="form-control" style="width:50px;display:inline;height:30px;" id="FillInBlanks" /> 题</h5>
        

        <h5 style="margin:10px;">
            试卷难度：
            <select name="difficulty" style="width:60%;height:30px;display:inline;" id="difficulty" class="form-control">
                <option value="middle">中难度</option>
                <option value="low">低难度</option>
                <option value="high">高难度</option>
            </select>
        </h5>
         <br />
         <br />
        <div class="panel panel-default" style="height:200px;">
            <form action="/Main/Choose">
                <h5 style="text-align:center">小知识点选了<b id="ChooseFewQuestions">0</b>个</h5>
                <div style="margin:10px;">
                    <h5 style="display:inline;">知识点：</h5>
                    <select class="form-control" id="BigKnowledge" style="display:inline;width:60%">
                        <option value="未分类">未分类</option>
                    </select>
                </div>
                <div style="text-align:center;padding-top:20px;">
                    <input type="button" class="layui-btn" value="全部小知识点" id="showAllSmallKnow" />
                    <input type="button" class="layui-btn" value="查询" id="selectKnow" />
                </div>
            </form>
        </div>
    </div>
</div>
<div class="col-md-9">
    <div class="panel panel-default" style="height:auto;">
        <div class="row" id="title">
            <div class="col-md-2 col-sm-2">

            </div>
            <div class="col-md-5 col-sm-5">大知识点</div>
            <div class="col-md-5 col-sm-5">小知识点</div>

            <hr />
        </div>
        <div id="table_body">

        </div>
    </div>

    <div id="demo1"></div>
</div>

<script>
    getKnowledge();
    getKnow();

    var knowledges;
    var know = [];//已选知识点

    // a：总题目  b:判断题  c:选择题  d：填空题
    var a = 20; b = 5, c = 10, d = 5;
    function totle() {
        //判断
        if (document.getElementById('Judge').value != "") {
            if (isNaN(document.getElementById('Judge').value))
            {
                layer.msg("输入判断题的题数需为数字");
                return false;
            }
            b = document.getElementById('Judge').value;
        }
        //选择
        if (document.getElementById('Choose').value != "") {
            if (isNaN(document.getElementById('Choose').value)) {
                layer.msg("输入选择题的题数需为数字");
                return false;
            }
            c = document.getElementById('Choose').value;
        }
        //填空
        if (document.getElementById('FillInBlanks').value != "") {
            if (isNaN(document.getElementById('FillInBlanks').value)) {
                layer.msg("输入填空题的题数需为数字");
                return false;
            }
            d = document.getElementById('FillInBlanks').value;
        }
        //总题目
        if (document.getElementById('Judge').value != "" || document.getElementById('Choose').value != "" || document.getElementById('FillInBlanks').value != "") {
            a = Number( document.getElementById('Judge').value) + Number(document.getElementById('Choose').value) + Number(document.getElementById('FillInBlanks').value);
        }
        x = document.getElementById('difficulty').value;
        return true;
    }

    function knowid(id, know) {
        $.ajax({
            url: '/Main/Yunyong',  // 跨域URL
            type: 'post',
            dataType: 'json',
            data: {
                id: id,
                knowId: know,
                questnumber: a,
                judgenumber: b,
                choosenumber: c,
                fillInBlanksnumber: d,
                difficultys:x
            },
            success: function (json) { //客户端jquery预先定义好的callback函数，成功获取跨域服务器上的json数据后，会动态执行这个callback函数
                time = json.time;
                ShowQuestion(json.question);
                layer.msg("因题库题目符合要求的不足，共替换了" + time + "道题。");
            },
        });
    }

    //获取所有知识点的数据
    function getKnowledge() {
        $.ajax({
            async: 'async',
            url: '/Main/GetKnowledge',
            type: 'get',
            dataType: 'json',
            timeout: 5000,
            success: function (json) {
                knowledges = json;
                showKnowledge(knowledges, "BigKnowledge");
            }
        });
    };

    //获取知识点
    function getKnow() {
        $.ajax({
            async: false,
            url: '/Main/ResualAllKnow',
            type: 'get',
            dataType: 'json',
            timeout: 5000,
            success: function (json) {
                knowledge = json;
                layOpen(knowledge);
            },
        });
    };

    //查询知识点
    $("#selectKnow").click(function () {
        var item = $("#BigKnowledge").find("option:selected").val();
        var knows = [];
        for (var i in knowledge) {
            if (item == knowledge[i].KnowID) {
                knows.push(knowledge[i].BigKnowID);
            }
        }
        if (knows.length == 0) {
            layer.msg("该知识点内无题目");
        } else {
            $("#table_body").html("");
            var newArr = isExistArr(knowledge, knows);
            layOpen(newArr);
        }
    });

    //显示所有知识点
    $("#showAllSmallKnow").click(function () {
        $("#table_body").html("");
        layOpen(knowledge);
    });


    function layOpen(data) {
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage, layer = layui.layer;
            laypage.render({
                elem: 'demo1'
                , count: data.length
                , jump: function (obj) {
                    document.getElementById('table_body').innerHTML = function () {
                        var arr = [], thisData = data.concat().splice(obj.curr * obj.limit - obj.limit, obj.limit);
                        layui.each(thisData, function (index, item) {
                            var items = '<div class="row">'
                                + '<div class="col-md-2 col-sm-2" style="text-align:center;">'
                                + '<input type="checkbox" onclick="IsChecked(this)" value="' + thisData[index].KnowID + '" />'
                                + '</div >'
                                + '<div class="col-md-5 col-sm-5">' + thisData[index].BigKnowName + '</div>'
                                + '<div class="col-md-5 col-sm-5">' + thisData[index].SmallKnowName + '</div>'
                                + '</div><hr style="margin: 10px 0;"/>';
                            arr.push(items);
                        });
                        return arr.join('');
                    }();
                }
            });
        });
    }

    //显示知识点
    function showKnowledge(data, knowledge) {
        $("#" + knowledge).empty();
        $.each(data, function (i, item) {
            $("<option></option>")
                .val(data[i].Value)
                .text(data[i].Text)
                .appendTo($("#" + knowledge))
        });
    }

    function isExistArr(arr1, arr2) {   // arr1原数组   arr2 要查找的数组 
        var new_arr = [];
        arr1.forEach(function (item, idx) {
            if (arr2.indexOf(item.BigKnowID) != -1) {
                new_arr.push(item);
            }
        })
        return new_arr;
    }

    function IsChecked(obj) {
        Checke = $(obj).prop('checked');
        var arr = [],x=0;
        if (Checke == true) {
            know.push($(obj).val());
        } else {
            var item = know.indexOf($(obj).val());
            delete know[item];
            for (var i in know) {
                if (know[i] == undefined) continue;
                arr[x++] = know[i];
            }
            know = arr;
        }
        
        document.getElementById('ChooseFewQuestions').innerHTML = know.length;
    };
</script>


